package com.lagou.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
@Configuration
@ConfigurationProperties(prefix = "spring.jedis.cluster")
public class JedisClusterConfig {

    private List<String> nodes;
    private String commandTimeout;

    @Bean
    public JedisCluster init() {
        Set<HostAndPort> clusterNode = new HashSet<>();
        for (String node : nodes) {
            String[] ipPortPair = node.split(":");
            clusterNode.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
        }
        System.out.println("init JedisCluster, nodes=" + String.join(",", nodes));
        return new JedisCluster(clusterNode, Integer.valueOf(commandTimeout));
    }

}
